<template>
  <div :class="`room-chat${otherClass}`" :title="data['time']">
    <div class="image">
      <img alt="Avatar" :src="data['avatar']" class="avatar"/>
      <div class="mask"/>
    </div>
    <div class="context">
      <span class="name" :style="`color: ${data['nameColor']}`">{{ data['username'] }}</span>
      <span style="display: inline-flex">
        <span v-for="item in data['identities']" :class="`identity identity_${item}`"></span>
      </span>
      <span>:</span>
      <span class="content" :style="`color: ${data['contentColor']}`">{{ msg }}</span>
    </div>
    <div class="mask"></div>
  </div>
</template>

<script setup>
import {computed, inject} from 'vue'

const props = defineProps({
  data: {
    type: Object,
    default: {}
  }
})
const config = inject('config')
const otherClass = computed(() => {
  let result = ''
  for (let identity of props.data['identities'])
    result += ' room-chat_' + identity
  return result
})
const msg = computed(() => {
  let result = props.data['content']
  for(let item of config.shield.Keyword) {
    let length = item.length
    let stars = ''
    for (let i = 0; i < length; i++) stars += '*'
    result = result.replace(item, stars)
  }
  return result
})
</script>

<style scoped lang="scss">
.room-chat {
  width: 100%;
  max-height: 200px;
  position: relative;
  padding: .6rem 1rem;
  margin: .2rem 0;
  display: flex;
  align-items: center;
  background: var(--z-green-color);
  color: var(--z-light-color);
  font-weight: bold;

  .image {
    flex-shrink: 0;
    position: relative;
    display: inline-flex;
    width: 3rem;
    height: 3rem;

    .avatar {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }

    .mask {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
    }
  }

  .context {
    flex: 1;
    flex-shrink: 0;
    margin-left: .8rem;
    line-height: 1.4rem;

    .name {
      margin-right: .4rem;
    }

    .badge {
      width: 1.8rem;
      height: 1.8rem;
      margin: 0 .1rem;
    }

    .content {
      margin-left: .4rem;
      word-break: break-all;
      line-break: anywhere;
    }
  }

  .mask {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
  }
}
</style>